﻿import yaml
import sys
import re
import string
class Phonem():
	def __init__(self):
		self.name = "phonemName"
		self.values = ['p1','p2']

class PhonemaLoader():
	def __init__(self):
		pass
		
	def load_phonema(self,phonema_file_name):
		file = open(phonema_file_name,'r')
		full_text = file.read()
		file.close()
		
		phonema =  yaml.load(full_text)
	
		return phonema
		
	
class Fonetik():
	
	def __init__(self):
		
		pl=PhonemaLoader()
		self.phonema = pl.load_phonema("phonema.yml")
	
	def translate(self,word):
		remain_word = string.lower(word)
		remain_word = re.sub('ñ','n',remain_word)
		remain_word = re.sub('á','a',remain_word)
		remain_word = re.sub('é','e',remain_word)
		remain_word = re.sub('í','i',remain_word)
		remain_word = re.sub('ó','o',remain_word)
		remain_word = re.sub('ú','u',remain_word)
		remain_word = re.sub('ü','u',remain_word)
		result_word = ''
		
		
		for phonem in self.phonema:
			
			for key in phonem.keys():
				for p in phonem[key]:
					
					result = re.search(str(p),remain_word)
					
					if result is not None:
						#print key + ": " + str(result.span()) + " Group:" + str(result.group(0))
						remain_word = re.sub(str(p),'',remain_word)
						result_word += key
						#print "["+result_word+"] [" + remain_word + "]"
		return result_word
		
		#for key in self.phonema.keys():
		#	print key
		#	#print "trying " + str(self.phonema[key])
		#	phonems = self.phonema[key]
		#	for p in phonems:
		#		result = re.search(str(p),remain_word)
		#		
		#		if result is not None:
		#			print key + ": " + str(result.span()) + " Group:" + str(result.group(0))
		#			remain_word = re.sub(str(p),'',remain_word)
		#			result_word += key
		#			print "["+result_word+"] [" + remain_word + "]"

def main():
  f=Fonetik()
  words = []
  if len(sys.argv) > 1:
	words = sys.argv
	del words[0]

  for word in words:
	f.translate(word)

if __name__ == '__main__':
  sys.exit(main())
